package com.smart.jetpackcomposestate.todo.one

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier
import com.smart.jetpackcomposestate.todo.TodoItem
import com.smart.jetpackcomposestate.todo.four.TodoViewModel
import com.smart.jetpackcomposestate.ui.theme.DN_Compose_LearnTheme

/**
 *
 * 27 - 无状态组件
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=27
 *
 * 31 - Compose 状态与状态提升
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=31
 *
 * 32 - 重组与remember
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=32
 *
 * 33 - MultableState
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=33
 *
 * 36 - 输入框下方图标
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=36
 *
 * 37 - 软键盘
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=37
 *
 *
 * 38 - 输入框背景
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=38
 *
 *
 * 39 - 编辑模式
 * https://www.bilibili.com/video/BV1ob4y1a7ad?spm_id_from=333.788.player.switch&vd_source=e07cc8cc94eb22bd76722071b03cd199&p=39
 *
 */
class TodoActivity : ComponentActivity() {

    val mTodoViewModel1 by viewModels<TodoViewModel>()
    val mTodoViewModel by viewModels<com.smart.jetpackcomposestate.todo.one.TodoViewModel>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
//            DN_Compose_LearnTheme {
//                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
////                    com.smart.jetpackcomposestate.todo.two.TodoItemInput(modifier = Modifier.padding(innerPadding)) { item ->
////                        Log.e("young_test90909","item = ${item.task}")
////                    }
//                }
//            }
            TodoActivityScreen1()
        }
    }

    @Composable
    fun TodoActivityScreen() {
//        val items = listOf(
//            TodoItem("Learn compose", TodoIcon.Event),
//            TodoItem("Take the codelab", TodoIcon.Event),
//            TodoItem("Apply state", TodoIcon.Event),
//            TodoItem("Build dynamic UIs", TodoIcon.Event),
//        )

        /**
         * 将 LiveData 转为 State 对象
         */
        val items: List<TodoItem> by mTodoViewModel.todoItems.observeAsState(listOf())

        DN_Compose_LearnTheme {
            Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                com.smart.jetpackcomposestate.todo.four.TodoScreen(
                    modifier = Modifier.padding(innerPadding),
                    items = items,
                    onAddItem = { todoItem: TodoItem ->
                        mTodoViewModel.addItem(todoItem)
                    },
                    onRemoveItem = { todoItem: TodoItem ->
                        mTodoViewModel.removeItem(todoItem)
                    },
                    currentlyEditing = null,
                    onStartEdit = {},
                    onEditItemChange = {},
                    onEditDone = {},
                    )
            }
        }
    }

    @Composable
    fun TodoActivityScreen1() {

        DN_Compose_LearnTheme {
            Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                com.smart.jetpackcomposestate.todo.four.TodoScreen(
                    modifier = Modifier.padding(innerPadding),
                    items = mTodoViewModel1.todoItems,
                    onAddItem = mTodoViewModel1::addItem,
                    onRemoveItem =mTodoViewModel1::removeItem,
                    currentlyEditing = mTodoViewModel1.currentEditItem,
                    onStartEdit = mTodoViewModel1::onEditItemSelected,
                    onEditItemChange = mTodoViewModel1::onEditItemChange,
                    onEditDone = mTodoViewModel1::onEditDone,
                )
            }
        }
    }
}
